import Vue from 'vue'
import axios from 'axios'
import axiosAdapterUniapp from 'axios-adapter-uniapp'

function convertObj(data) {
	var _result = [];
	for (var key in data) {
		var value = data[key];
		if (value.constructor == Array) {
			value.forEach(function(_value) {
				_result.push(key + "=" + _value);
			});
		} else {
			_result.push(key + '=' + value);
		}
	}
	return _result.join('&');
}
// create an axios instance
const service = axios.create({
	baseURL: uni.baseURL, // url = base url + request url
	//withCredentials: true, // send cookies when cross-domain requests 注意：withCredentials和后端配置的cross跨域不可同时使用
	timeout: 60000, // request timeout
	crossDomain: true,
	adapter: axiosAdapterUniapp
})

// request拦截器,在请求之前做一些处理
service.interceptors.request.use(
	config => {
		if (uni.getStorageSync('token')) {
			// 给请求头添加user-token
			config.headers["token"] = uni.getStorageSync('token');
			// config.headers["token"] =
			// 	'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNzc1MzY0OTYyMiIsImNyZWF0ZWQiOjE2NDEzNDg1MDc5MzAsImV4cCI6MTY0MTk1MzMwN30.gy2dTDfaS0LS39P9KWgqWeRkainYf_fBvipbUgfDzCpqyuf6yau7ODu0yKozeBrthozQi4vTsPvjASTvHlDVUQ'
		}
		return config;
	},
	error => {
		console.log(error); // for debug
		return Promise.reject(error);
	}
);

//配置成功后的拦截器
service.interceptors.response.use(res => {
	if (uni.versiontype) {
		let obj = JSON.parse(uni.getStorageSync('versiontype'))
		setTimeout(() => {
			uni.showModal({
				title: '提示',
				content: '发现新版本' + obj.version + '！',
				showCancel: false,
				success: function(ress) {
					if (ress.confirm) {
						plus.runtime.openURL(obj.fullPath, function(res) {

						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},1000)
	}
	if (res.statusText == 'request:fail') {
		uni.showToast({
			title: '网络异常',
			duration: 2000,
			icon: 'none'
		});
	}
	if (res.data.code == 401) {
		let routes = getCurrentPages()
		let curRoute = routes[routes.length - 1]
		let Url = undefined;
		// console.log(curRoute.route)
		// console.log(123123)
		if (curRoute) {
			// #ifdef H5
			Url = curRoute.route + '?pageLinkType=' + (curRoute.$vm.LinkType || '') + '&' + convertObj(curRoute
				.options)
			// #endif
			// #ifndef H5
			Url = curRoute.route + '?pageLinkType=' + (curRoute.$vm.LinkType || '') + '&' + convertObj(curRoute
				.options)
			// #endif
		}
		if (curRoute.route == 'pages/login/login') {
			uni.reLaunch({
				url: '/pages/login/login?invitacode=' + (curRoute.options.invitacode || '')
			});
			return res.data
		}
		if (Url) {
			setTimeout(() => {
				uni.reLaunch({
					url: '/pages/login/login?Url=' + encodeURIComponent(Url) + '&invitacode=' +
						(curRoute.options.invitacode || '')
				});
			}, 100)
		} else {
			uni.reLaunch({
				url: '/pages/login/login?invitacode=' + (curRoute.options.invitacode || '')
			});
		}

	}

	return res.data
}, error => {
	uni.showToast({
		title: '网络异常',
		duration: 2000,
		icon: 'none'
	});
	return Promise.reject(error)
})


export default service
